﻿using System;
using System.Windows;
using System.Windows.Controls;
using churchtool.Plugin.SermonUpload.View.Services;
using churchtool.Plugin.SermonUpload.ViewModel;

namespace churchtool.Plugin.SermonUpload.View
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class UploadWindowView : UserControl
    {
        #region Constructor

        public UploadWindowView()
        {
            InitializeComponent();
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Subscribes to View Model events when data context is set.
        /// </summary>
        /// <remarks>
        /// This method subscribes to the view model's WorkStarting and
        /// WorkEnding events to control display of the Progress dialog
        /// provided by the application.
        /// </remarks>
        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var viewModel = (UploadWindowViewModel)e.NewValue;
            if (viewModel != null)
            {
                viewModel.WorkStarted += OnWorkStarting;
                viewModel.WorkEnded += OnWorkEnding;
            }
        }

        /// <summary>
        /// Closes the Progress dialog.
        /// </summary>
        private void OnWorkEnding(object sender, EventArgs e)
        {
            ViewServices.CloseProgressDialog();
        }

        /// <summary>
        /// Displays the Progress dialog.
        /// </summary>
        private void OnWorkStarting(object sender, EventArgs e)
        {
            var mainWindowViewModel = (UploadWindowViewModel) sender;
            var progressDialogViewModel = mainWindowViewModel.ProgressDialogViewModel;
            ViewServices.ShowProgressDialog(Window.GetWindow(this), progressDialogViewModel);
        }

        #endregion
    }
}